const {success,error,successWithData} = require("../utils/resultwrapper")
const dictService = require("../services/dictService");

const dictController = {
    async getAllDicts(req, res, next) {
        try {
            const dicts = await dictService.getAllDicts();
            res.json(successWithData(dicts));
        } catch (err) {
            return res.json(error(err.message))
        }
    },
  
    async getDicByPage(req, res, next) {
        const {pageNum, pageSize, name, dictCode, status} = req.query;
        try {
            const result = await dictService.getDicByPage(pageNum, pageSize, name, dictCode, status);
            return res.json(successWithData(result))
        } catch (err) {
            return res.json(error(err.message))
        }
    },

    async addDict(req, res, next) {
        try {
            const result = await dictService.addDict(req.body);
            return res.json(successWithData(result))
        } catch (err) {
            return res.json(error(err.message))
        }
    },

    async deleteDict(req, res, next) {
        try {
            await dictService.deleteDict(req.params.id);
            return res.json(success())
        } catch (err) {
            return res.json(error(err.message))
        }
    },

    async updateDict(req, res, next) {
        try {
            const { id } = req.params;
            const dictData = req.body;
            const result = await dictService.updateDict(id, dictData);
            return res.json(successWithData(result))
        } catch (err) {
            return res.json(error(err.message))
        }
    },

    async getDictById(req, res, next) {
        try {
            const { id } = req.params;
            const result = await dictService.getDictById(id);
            return res.json(successWithData(result))
        } catch (err) {
            return res.json(error(err.message))
        }
    }
};

module.exports = dictController;